/* $Id: Restore.java 62 2008-12-19 01:09:20Z rmceoin $
 * 
 * Copyright 2008 Randy McEoin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bitsetters.android.passwordsafe;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Restore extends Activity {
	
	private static boolean debug = false;
	private static final String TAG = "Restore";
	
	private DBHelper dbHelper=null;
	private String masterKey="";
	private String filename=null;
	private RestoreDataSet restoreDataSet=null;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		if (debug) Log.d(TAG,"onCreate()");

		if (!CategoryList.isSignedIn()) {
			Intent frontdoor = new Intent(this, FrontDoor.class);
			startActivity(frontdoor);		
			finish();
		}

		setContentView(R.layout.restore);
		String title = getResources().getString(R.string.app_name) + " - " +
			getResources().getString(R.string.restore);
		setTitle(title);
		
		if (filename==null) {
			filename=this.getDir("data", 0) + "/" + CategoryList.BACKUP_FILENAME;
		}
		TextView filenameText;
		filenameText = (TextView) findViewById(R.id.restore_filename);
		filenameText.setText(filename);

		TextView restoreInfoText;
		restoreInfoText = (TextView) findViewById(R.id.restore_info);

		EditText passwordText;
		passwordText = (EditText) findViewById(R.id.restore_password);
		
		Button restoreButton;
		restoreButton = (Button) findViewById(R.id.restore_button);

		if (!backupFileExists(filename)) {
			passwordText.setVisibility(0);
			restoreButton.setVisibility(0);
			restoreInfoText.setText(R.string.restore_no_file);
			return;
		}

		restoreButton.setVisibility(1);

		restoreButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				read(filename);
			}
		});
    }

    @Override
	protected void onResume() {
		super.onResume();
		
		if (debug) Log.d(TAG,"onResume()");
		
		if (!CategoryList.isSignedIn()) {
			Intent frontdoor = new Intent(this, FrontDoor.class);
			startActivity(frontdoor);		
			finish();
		}
	}

    private boolean backupFileExists(String filename) {
		FileReader fr;
		try {
			fr = new FileReader(filename);
			fr.close();
		} catch (FileNotFoundException e) {
			return false;
		} catch (IOException e) {
			return false;
		}
		return true;
    }

    public boolean read(String filename) {
		if (debug) Log.d(TAG,"read("+filename+",)");

		
		try {
			if (dbHelper == null){
				dbHelper = new DBHelper(this);
			}
			dbHelper.restore(filename);
		} catch (Exception e1) {
			Toast.makeText(Restore.this, getString(R.string.restore_error)+
				" "+e1.getLocalizedMessage(),
				Toast.LENGTH_LONG).show();
			return false;
		}

		Toast.makeText(Restore.this, getString(R.string.restore_complete),
				Toast.LENGTH_LONG).show();
		return true;
	}
    
	private void restoreDatabase() {
		dbHelper.close();
		String dest = dbHelper.db4oDBFullPath(this);
		dbHelper.deleteDatabase();
		new File(filename).renameTo(new File(dest));
		
		Toast.makeText(Restore.this, getString(R.string.restore_complete),
			Toast.LENGTH_LONG).show();

		setResult(RESULT_OK);
		finish();
	}
}
